﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace HYRElectric.PowerSystem
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdSystemLoop : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			Document document = activeUIDocument.Document;
			FamilyInstance familyInstance = null;
			try
			{
				SwitchBoxFilter switchBoxFilter = new SwitchBoxFilter();
				RevitVersionFuncs.ClearSelection(selection);
				Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, switchBoxFilter, "请选择配电箱");
				if (reference == null)
				{
					return Autodesk.Revit.UI.Result.Failed;
				}
				familyInstance = (document.GetElement(reference) as FamilyInstance);
				DistributionSysType panelDistributionSysType = this.GetPanelDistributionSysType(document, familyInstance);
				if (panelDistributionSysType != null)
				{
					Transaction transaction = new Transaction(document, "DistributionSys");
					transaction.Start();
					try
					{
						familyInstance.get_Parameter(BuiltInParameter.RBS_FAMILY_CONTENT_DISTRIBUTION_SYSTEM).Set(panelDistributionSysType.Id);
						transaction.Commit();
					}
					catch
					{
						transaction.RollBack();
						return Autodesk.Revit.UI.Result.Failed;
					}
				}
			}
			catch
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			DlgSystemLoop dlgSystemLoop = new DlgSystemLoop(commandData, familyInstance);
			for (;;)
			{
				DialogResult dialogResult = dlgSystemLoop.ShowDialog();
				if (dialogResult == DialogResult.Cancel)
				{
					break;
				}
				if (dialogResult == DialogResult.OK)
				{
					IList<Reference> lstEquips = null;
					try
					{
						ElectricFilter electricFilter = new ElectricFilter();
						RevitVersionFuncs.ClearSelection(selection);
						lstEquips = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, electricFilter, "请选择电气设备");
					}
					catch (Exception ex)
					{
						string message2 = ex.Message;
						continue;
					}
					uint nOkType = dlgSystemLoop.m_nOkType;
					if (nOkType != 0u)
					{
						if (nOkType == 1u)
						{
							dlgSystemLoop.AddEquipsToLoop(lstEquips);
						}
					}
					else
					{
						dlgSystemLoop.AddNewLoop(lstEquips);
					}
				}
				else if (dialogResult == DialogResult.Yes)
				{
					LoopInfoItem loopInfoItem = dlgSystemLoop.m_dicloopInfo[dlgSystemLoop.m_strKey];
					this.DrawMaterialList(commandData, dlgSystemLoop.m_loopInfoManager.GetPanelNumber(), dlgSystemLoop.m_strKey, loopInfoItem.dPower, dlgSystemLoop.m_lstMaterial);
				}
			}
			dlgSystemLoop.Close();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void DrawMaterialList(ExternalCommandData commandData, string strSwitchbox, string strLoop, double dPower, List<MaterialInfo> lstInfo)
		{
			MaterialListOutput materialListOutput = new MaterialListOutput(commandData);
			string text = "";
			materialListOutput.Drawing(strSwitchbox, strLoop, dPower, lstInfo, ref text);
		}

		private DistributionSysType GetPanelDistributionSysType(Document doc, FamilyInstance fi)
		{
			Family family = fi.Symbol.Family;
			Document document = doc.EditFamily(family);
			if (document == null || !document.IsFamilyDocument)
			{
				TaskDialog.Show("盈建科提示", "it's family document");
				return null;
			}
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(ConnectorElement)).ToElements();
			int num = 0;
			double num2 = 0.0;
			foreach (Element element in enumerable)
			{
				ConnectorElement connectorElement = element as ConnectorElement;
				if (connectorElement.Domain == (Autodesk.Revit.DB.Domain)2)
				{
					num2 = connectorElement.GetParameter(BuiltInParameter.RBS_ELEC_VOLTAGE).AsDouble();
					num = connectorElement.GetParameter(BuiltInParameter.RBS_ELEC_NUMBER_OF_POLES).AsInteger();
					break;
				}
			}
			ICollection<Element> collection = new FilteredElementCollector(doc).OfClass(typeof(DistributionSysType)).ToElements();
			if (collection.Count == 0)
			{
				return null;
			}
			foreach (Element element2 in collection)
			{
				DistributionSysType distributionSysType = element2 as DistributionSysType;
				distributionSysType.get_Parameter(BuiltInParameter.RBS_DISTRIBUTIONSYS_PHASE_PARAM).AsInteger();
				try
				{
					if (num != 1)
					{
						if (num == 3)
						{
							ElementId elementId = distributionSysType.get_Parameter(BuiltInParameter.RBS_DISTRIBUTIONSYS_VLL_PARAM).AsElementId();
							if (!(elementId == ElementId.InvalidElementId))
							{
								VoltageType voltageType = doc.GetElementById(elementId) as VoltageType;
								if (num2 >= voltageType.get_Parameter(BuiltInParameter.RBS_VOLTAGETYPE_MINVOLTAGE_PARAM).AsDouble() && num2 <= voltageType.get_Parameter(BuiltInParameter.RBS_VOLTAGETYPE_MAXVOLTAGE_PARAM).AsDouble())
								{
									return distributionSysType;
								}
							}
						}
					}
					else
					{
						ElementId elementId2 = distributionSysType.get_Parameter(BuiltInParameter.RBS_DISTRIBUTIONSYS_VLG_PARAM).AsElementId();
						if (!(elementId2 == ElementId.InvalidElementId))
						{
							VoltageType voltageType2 = doc.GetElementById(elementId2) as VoltageType;
							if (num2 >= voltageType2.get_Parameter(BuiltInParameter.RBS_VOLTAGETYPE_MINVOLTAGE_PARAM).AsDouble() && num2 <= voltageType2.get_Parameter(BuiltInParameter.RBS_VOLTAGETYPE_MAXVOLTAGE_PARAM).AsDouble())
							{
								return distributionSysType;
							}
						}
					}
				}
				catch (Exception)
				{
					return null;
				}
			}
			return null;
		}
	}
}
